/*
 * Timer.cpp
 *
 *  Created on: Oct 16, 2012
 *      Author: travis
 */

#include "Timer.h"

Timer::~Timer()
{
}

Timer::Timer()
{
	//Initialize the variables
	startTicks = 0;

	deltaclock = 0;
	currentFPS = 0;

	lastTimeIntervalCHCK = SDL_GetTicks();
}

// cout curent fps (paste in main loop)
Uint32 Timer::update()
{
	deltaclock = SDL_GetTicks() - startTicks;
	startTicks = SDL_GetTicks();

	if (deltaclock != 0)
		currentFPS = 1000 / deltaclock;

	return currentFPS;
}

float Timer::getDeltaClock()
{
	return (float) deltaclock / 1000.0;
}

unsigned int Timer::getDeltaClockMs()
{
	return deltaclock;
}

bool Timer::GetInterval(unsigned int interval)
{
	if ((lastTimeIntervalCHCK + interval) < SDL_GetTicks())
	{
		lastTimeIntervalCHCK = SDL_GetTicks();
		return true;
	}
	else
		return false;
}

void Timer::resetTimer()
{
	lastTimeIntervalCHCK = SDL_GetTicks();
}

void Timer::bindClass(LuaScript::LuaScript * lScript)
{
	lua_State * luaState = lScript->getLuaState();
	luabind::object * table = lScript->getTable();

	luabind::module(luaState)[luabind::class_ < Timer
			> ("Timer").def(luabind::constructor<>()).def("GetInterval",
					&Timer::GetInterval).def("resetTimer", &Timer::resetTimer).def(
					"update", &Timer::update)];

	// push this object reference to lua script
	table[0]["timing"] = this;

}

